package com.changgou.file.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.file.util.FastDFSClient;
import com.changgou.file.util.FastDFSFile;
import com.sun.org.apache.bcel.internal.generic.I2F;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("/file")
public class FileController {

    /**
     * 上传文件
     *
     * @param file 上传的文件
     * @return
     */
    @PostMapping("/upload")
    public Result uploadFile(MultipartFile file) {
        try {
            //判断文件是否存在
            if (file == null) {
                throw new RuntimeException("文件不存在");
            }
            //获取文件的完整名称
            String originalFilename = file.getOriginalFilename();
            if (StringUtils.isEmpty(originalFilename)) {
                throw new RuntimeException("文件不存在");
            }

            //获取文件的扩展名称  abc.jpg   jpg
            String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);

            //获取文件内容
            byte[] content = file.getBytes();

            //创建文件上传的封装实体类
            FastDFSFile fastDFSFile = new FastDFSFile(originalFilename, content, extName);

            //基于工具类进行文件上传,并接受返回参数  String[]
            String[] uploadResult = FastDFSClient.upload(fastDFSFile);

            //封装返回结果
            String url = FastDFSClient.getTrackerUrl() + uploadResult[0] + "/" + uploadResult[1];

            return new Result(true, StatusCode.OK, "文件上传成功", url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, StatusCode.ERROR, "文件上传失败");
    }

    /**
     * 删除文件
     * @param group1 组名
     * @param remoteFileName 1，2级文件夹名称+文件名称
     * @return
     */
    @DeleteMapping("/delete")
    public Result deleteFile(@RequestParam("remoteFileName") String remoteFileName) {
        try {
            if (remoteFileName != null) {
                //
                FastDFSClient.deleteFile("group1", remoteFileName);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "文件删除失败");
        }

        return new Result(true, StatusCode.OK, "文件删除成功");
    }

    /**
     * 下载文件
     *
     * @param response
     * @param request
     * @return
     * @throws IOException //"http://192.168.200.128:8080/group1/M00/00/00/wKjIgF9aJHmANYtnAANLge_MWvY079.png"
     */

    @GetMapping("/download")
    public Result download(HttpServletResponse response, HttpServletRequest request) throws IOException {
        //组名
        String groupName = "group1";
        //文件存储完整名
        String remoteFileName = "M00/00/00/wKjIgF9aJHmANYtnAANLge_MWvY079.png";
        //响应输出流对象
        ServletOutputStream out = response.getOutputStream();
        //获取文件的类型
        String mimeType = request.getServletContext().getMimeType(".png");
        //设置响应文件类型
        response.setContentType(mimeType);
        //设置文件打开方式为下载附件的形式
        response.setHeader("content-Disposition", "attachment;filename=report.png");
        //获得下载文件的输入流
        InputStream inputStream = FastDFSClient.downFile(groupName, remoteFileName);
        //将输入流写到响应输出流中
        IOUtils.copy(inputStream, out);
        //返回结果到前端
        return new Result(true, StatusCode.ERROR, "文件下载成功");
    }
}
